<script type="text/javascript">

    var envidmap = new Map()
    $(function () {

        // 加载Heimdallr服务器系统状态信息
        $.get({{urlfor "StatisticalController.SysInfo"}},function (res) {
            //console.log(res)
            // cpu颜色
            if (parseFloat(res["Cpu"]) <= 70 ) { COLOR_CPU = "bg-green"}
            if ( 70 < parseFloat(res["Cpu"]) && parseFloat(res["Cpu"]) <= 90) { COLOR_CPU = "bg-yellow" }
            if (parseFloat(res["Cpu"]) > 90) { COLOR_CPU = "bg-red" }
            // 内存颜色
            if (parseFloat(res["Mem"]) <= 70 ) { COLOR_MEM = "bg-green"}
            if ( 70 < parseFloat(res["Mem"]) && parseFloat(res["Mem"]) <= 90) { COLOR_MEM = "bg-yellow" }
            if (parseFloat(res["Mem"]) > 90) { COLOR_MEM = "bg-red" }
            // 磁盘颜色
            if (parseFloat(res["Disk"]) <= 70 ) { COLOR_DISK = "bg-green"}
            if ( 70 < parseFloat(res["Disk"]) && parseFloat(res["Disk"]) <= 90) { COLOR_DISK = "bg-yellow" }
            if (parseFloat(res["Disk"]) > 90) { COLOR_DISK = "bg-red" }

            var el = $('<div class="col-md-3">\n<div class="card card-widget widget-user-2">\n' +
                '<div class="widget-user-header bg-green">\n' +
                '<h3>Heimdallr信息系统</h3>\n\<p>版本: '+res["Version"]+'</p></div>\n' +
                '<div class="card-footer p-1">\n<ul class="nav flex-column">\n' +
                '<li class="nav-item">\n<a href="#" class="nav-link">\n时间 <span id="time" class="pull-right label bg-blue ">'+res["Timer"]+'</span>\n</a>\n</li>\n' +
                '<li class="nav-item">\n<a href="#" class="nav-link">\n系统 <span class="pull-right label bg-blue">'+res["Osinfo"]+'</span>\n</a>\n</li>\n' +
                '<li class="nav-item">\n<a href="#" class="nav-link">\nCPU <span class="pull-right label '+ COLOR_CPU +'">'+res["Cpu"]+'%</span>\n</a>\n</li>\n' +
                '<li class="nav-item">\n<a href="#" class="nav-link">\n内存 <span class="pull-right label '+ COLOR_MEM +'">'+res["Mem"]+'%</span>\n</a>\n</li>\n' +
                '<li class="nav-item">\n<a href="#" class="nav-link">\n磁盘 <span class="pull-right label '+ COLOR_DISK +'">'+res["Disk"]+'%</span>\n</a>\n</li>\n</ul>\n</div>\n</div>\n</div>')
            el.prependTo($('#sysinfobox'))
        })

        // 根据环境自动加载各个环境的bifrost状态
        $.get({{urlfor "StatisticalController.Bifrost"}},function (res) {
            //console.log(res)
            $.each(res,function (i,item) {
                envidmap.set(item["Env"],item["Id"])
                if (item["Enable"] === 1) {
                    COLOR = "bg-aqua-active"
                    ENVNAME = item["Env"]
                    if (item["status"] === "TimeOut") {item["Env"]+="(连接超时)"}
                    // cpu颜色
                    if (parseFloat(item["message"]["cpu"]) <= 70 ) { COLOR_CPU = "bg-green"}
                    if ( 70 < parseFloat(item["message"]["cpu"]) && parseFloat(item["message"]["cpu"]) <= 90) { COLOR_CPU = "bg-yellow" }
                    if (parseFloat(item["message"]["cpu"]) > 90) { COLOR_CPU = "bg-red" }
                    // 内存颜色
                    if (item["message"]["mem"] <= 70 ) { COLOR_MEM = "bg-green"}
                    if ( 70 < item["message"]["mem"] && item["message"]["mem"] <= 90) { COLOR_MEM = "bg-yellow" }
                    if (item["message"]["mem"] > 90) { COLOR_MEM = "bg-red" }
                    // 磁盘颜色
                    if (item["message"]["disk"] <= 70 ) { COLOR_DISK = "bg-green"}
                    if ( 70 < item["message"]["disk"] && item["message"]["disk"] <= 90) { COLOR_DISK = "bg-yellow" }
                    if (item["message"]["disk"] > 90) { COLOR_DISK = "bg-red" }

                    var el = $('<div class="col-md-3">\n<div class="card card-widget widget-user-2">\n' +
                        '<div class="widget-user-header ' + COLOR + '"name=agent id=' + ENVNAME+'>\n' +
                        '<h3>Bifrost: '+item["Env"]+'</h3>\n\<p>版本: '+item["message"]["bifrost_version"]+'</p></div>\n' +
                        '<div class="card-footer p-1">\n<ul class="nav flex-column">\n' +
                        '<li class="nav-item">\n<a href="#" class="nav-link">\n时间 <span id="time" class="pull-right label bg-blue ">'+item["message"]["time"]+'</span>\n</a>\n</li>\n' +
                        '<li class="nav-item">\n<a href="#" class="nav-link">\n系统 <span class="pull-right label bg-blue">'+item["message"]["system"]+'</span>\n</a>\n</li>\n' +
                        '<li class="nav-item">\n<a href="#" class="nav-link">\nCPU <span class="pull-right label '+ COLOR_CPU +'">'+item["message"]["cpu"]+'%</span>\n</a>\n</li>\n' +
                        '<li class="nav-item">\n<a href="#" class="nav-link">\n内存 <span class="pull-right label '+ COLOR_MEM +'">'+item["message"]["mem"]+'%</span>\n</a>\n</li>\n' +
                        '<li class="nav-item">\n<a href="#" class="nav-link">\n磁盘 <span class="pull-right label '+ COLOR_DISK +'">'+item["message"]["disk"]+'%</span>\n</a>\n</li>\n</ul>\n</div>\n</div>\n</div>')
                    el.appendTo($('#sysinfobox'))
                }else {
                    COLOR = "bg-gray-active"
                    item["Env"]+="(已禁用)"
                    var el = $('<div class="col-md-3">\n<div class="card card-widget widget-user-2">\n' +
                        '<div class="widget-user-header ' + COLOR + '">\n' +
                        '<h3>Bifrost: '+item["Env"]+'</h3>\n\<p>版本: nil </p></div>\n' +
                        '<div class="card-footer p-1">\n<ul class="nav flex-column">\n' +
                        '<li class="nav-item">\n<a href="#" class="nav-link">\n时间 <span id="time" class="pull-right label bg-blue ">nil</span>\n</a>\n</li>\n' +
                        '<li class="nav-item">\n<a href="#" class="nav-link">\n系统 <span class="pull-right label bg-blue">nil</span>\n</a>\n</li>\n' +
                        '<li class="nav-item">\n<a href="#" class="nav-link">\nCPU <span class="pull-right label bg-green">nil</span>\n</a>\n</li>\n' +
                        '<li class="nav-item">\n<a href="#" class="nav-link">\n内存 <span class="pull-right label bg-yellow">nil</span>\n</a>\n</li>\n' +
                        '<li class="nav-item">\n<a href="#" class="nav-link">\n磁盘 <span class="pull-right label bg-red">nil</span>\n</a>\n</li>\n</ul>\n</div>\n</div>\n</div>')
                    el.appendTo($('#sysinfobox'))
                }

            })
        });
        //document.getElementById(agent)
        $('#sysinfobox').on("click","div[name=agent]",
        function () {
            $.cookie('env',encodeURI($(this).attr('id')),{ expires: 7, path: '/' });
            $.cookie('env_id',encodeURI(envidmap.get($(this).attr('id'))),{ expires: 7, path: '/' });
            window.location.href={{urlfor "DetailController.Index"}}
        })
    })
</script>
<!-- 显示nginxconf配置统计信息 -->
